<?php

namespace App\Repositories\Goods;

use Illuminate\Support\Facades\DB;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Models\Goods\Goods;
use App\Validators\GoodsValidator;

/**
 * Class GoodsRepositoryEloquent
 * @package namespace App\Repositories;
 */
class GoodsRepositoryEloquent extends BaseRepository implements GoodsRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Goods::class;
    }

    /**
     * @author Jerry
     * @param int $goodsId
     * @return mixed
     */
    public function findGoodsPromoteInfoByGoodsId(int $goodsId)
    {
        return $this->findWhere(['goods_id' => $goodsId], [
            'goods_id',
            'is_promote',
            'promote_price',
            'promote_start_date',
            'promote_end_date',
            'promote_goods_number',
            'promote_today_number',
            'promote_number',
            'is_buynow',
        ]);
    }

    /**
     * 获取商品虚拟库存
     * @param int $goodsId
     * @param int $wid
     * @return mixed
     */
    public function getGoodsVirtualStock(int $goodsId,int $wid){
        $goods_stock = DB::table('goods_wid_'.$wid)->where('goods_id',$goodsId)->value('goods_number');
        return $goods_stock;
    }

    /**
     * @author yangzhic
     * @param array $goodsIds
     * @return mixed
     */
    public function getGoodsByIds(array $goodsIds){
        return $this->findWhereIn('goods_id',$goodsIds);
    }

    /**
     * @role 获取会员价
     * @author Jianhe
     * @param int $goodsId
     * @param int $userRank
     * @return mixed
     */
    public function findGoodsRankPrice(int $goodsId,int $userRank){
        return DB::table('member_price')
            ->where("goods_id",$goodsId)
            ->where("user_rank",$userRank)
            ->value("user_price");
    }

    /**
     * @author yangzhic
     * @param int $goodsId
     * @param int $userRank 会员等级
     * @return array
     */
    public function getGoodsPrice(int $goodsId,int $userRank)
    {
        $fields = [
            'g.goods_id',
            'g.shop_price',
            'mp.user_price',
            'uk.discount'
        ];

        return DB::table('member_price as mp')
            ->leftJoin('goods as g','mp.goods_id','=','g.goods_id')
            ->leftJoin('user_rank as uk','uk.rank_id','=','mp.user_rank')
            ->where('mp.user_rank','=',$userRank)
            ->where('g.goods_id','=',$goodsId)
            ->select($fields)
            ->first();
    }

    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

    public function getByIdsFromWid(array $goodsIds, int $wid){
        return DB::table('goods_wid_'.$wid)->whereIn('goods_id',$goodsIds)->get()->toArray();
    }

    /**
     * 返回按商品id搜索结果
     * @author Min
     * @param $goods_ids
     * @param $uid
     * @param $wid
     * @return array
     */
    public function getSearchGoodsByIid($goods_ids, $uid, $wid)
    {

        $where = "g.goods_id in($goods_ids) AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND g.goods_thumb !=''";

        $sql = "SELECT g.*,c.cat_name,
                IF (g.practical_num > 0, 1, 0) AS real_num,
                IF (g.goods_number > 0, 1, 0) AS fic_num
                FROM
                    sdx_goods_wid_{$wid} as g 
                    LEFT JOIN sdx_category c ON g.cat_id = c.cat_id
                WHERE {$where} order by field(g.goods_id, {$goods_ids})";

        $data = DB::select($sql);

        return array_map('get_object_vars', $data);

    }
}
